﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Capstone_project.DatabaseAccessLayer;
using Capstone_project.BussnessLogicLayer;

namespace Capstone_project
{
    public partial class frmCreateNewTaskgroup : Form
    {
        TaskGroupBUS taskgroupbus = new TaskGroupBUS();
        ProjectBUS projectbus = new ProjectBUS();
        DataTable dt=new DataTable();
        DataTable dtproject = new DataTable();
        SqlDataAdapter adap;
        SqlConnection con = Common.con;
        public static string idproject;
        public static string nameproject;
        public frmCreateNewTaskgroup()
        {
            InitializeComponent();
        }

        private void frmCreateNewTaskgroup_Load(object sender, EventArgs e)
        {
            txtTaskgroupNAME.Focus();
            if (idproject != null)
            {
                string sqlpro = "SELECT * FROM PROJECT WHERE ID_PROJECT='"+idproject+"'";
                if (con.State != ConnectionState.Open)
                    con.Open();
                SqlCommand cmd = new SqlCommand(sqlpro,con);
                dtproject=new DataTable();
                adap = new SqlDataAdapter(cmd);
                adap.Fill(dtproject);
                con.Close();
                cbxProject.DataSource = dtproject;
                cbxProject.ValueMember = "ID_PROJECT";
                cbxProject.DisplayMember = "NAME_PROJECT";
                cbxProject.Enabled = false;

            }
            else if(idproject == null)
            {
                cbxProject.Enabled = true;
                string sqlpro1 = "SELECT * FROM PROJECT";
                if (con.State != ConnectionState.Open)
                    con.Open();
                SqlCommand cmd = new SqlCommand(sqlpro1, con);
                dtproject = new DataTable();
                adap = new SqlDataAdapter(cmd);
                adap.Fill(dtproject);
                con.Close();
                cbxProject.DataSource = dtproject;
                cbxProject.ValueMember = "ID_PROJECT";
                cbxProject.DisplayMember = "NAME_PROJECT";
            }
            txtTaskgroupID.Text = taskgroupbus.nextID();
            txtTaskgroupID.Enabled = false;
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            try
            {
                string sql = "INSERT INTO TASK_GROUP (ID_TASK_GROUP,NAME_TASK_GROUP,ID_PROJECT) VALUES ('" + txtTaskgroupID.Text + "','" + txtTaskgroupNAME.Text + "','" + cbxProject.SelectedValue + "')";
                if(con.State!=ConnectionState.Open)
                    con.Open();
                SqlCommand com = new SqlCommand(sql, con);
                int kq = com.ExecuteNonQuery();
                con.Close();
                if (kq != 0)
                {
                    idproject = null;
                    frmAddTask.idtaskgroup = txtTaskgroupID.Text;
                    this.Close();
                    frmNextstepTask nextsteptask = new frmNextstepTask();
                    nextsteptask.ShowDialog();
                   
                }
            }
            catch
            {
                MessageBox.Show("Fail to create new Task group");
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
